<?php

use app\common\enum\DeliveryType as DeliveryTypeEnum;

?>
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-body am-fr">
                    <!-- 工具栏 -->
                    <div class="page_toolbar am-margin-bottom-xs am-cf">
                        <form id="form-search" class="toolbar-form" action="">
                            <input type="hidden" name="s" value="/<?= $request->pathinfo() ?>">
                            <input type="hidden" name="dataType" value="<?= $dataType ?>">
                            <div class="am-u-sm-12 am-u-md-3">
                                <div class="am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-btn-group am-btn-group-xs">
                                            <?php if (checkPrivilege('order/export')): ?>
                                                <a class="j-export am-btn am-btn-success am-radius"
                                                   href="javascript:void(0);">
                                                    <i class="iconfont icon-daochu am-margin-right-xs"></i>订单导出
                                                </a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
<!--                            <div class="am-u-sm-12 am-u-md-9">-->
<!--                                <div class="am fr">-->
<!--                                    <div class="am-form-group am-fl">-->
<!--                                        <div class="am-input-group am-input-group-sm tpl-form-border-form">-->
<!--                                            <input type="text" class="am-form-field" name="search"-->
<!--                                                   placeholder="请输入订单号/用户昵称" value="">-->
<!--                                            <div class="am-input-group-btn">-->
<!--                                                <button class="am-btn am-btn-default am-icon-search" type="submit"></button>-->
<!--                                            </div>-->
<!--                                        </div>-->
<!--                                    </div>-->
<!--                                </div>-->
<!--                            </div>-->
                        </form>
                    </div>
                    <div class="order-list am-scrollable-horizontal am-u-sm-12 am-margin-top-xs">
                        <table width="100%" class="am-table am-table-centeredam-text-nowrap am-margin-bottom-xs">
                            <thead>
                                <tr>
                                    <th width="20%" class="goods-detail">商品信息</th>
                                    <th>单价/数量</th>
                                    <th>商家</th>
                                    <th>总费用</th>
                                    <th>实际支付</th>
                                    <th>状态</th>
                                    <th>操作</th>

                                </tr>
                            </thead>
                            <tbody>

                                <?php $colspan = 8; ?>
                                <?php foreach ($arr as $order): ?>

                                    <?php if (!$order->isEmpty()): ?>
                                        <tr class="order-empty">
                                            <td colspan="<?= $colspan ?>"></td>
                                        </tr>
                                        <tr>
                                            <td class="am-text-middle am-text-left" colspan="<?= $colspan ?>">
                                                <span class="am-margin-right-lg">订单号：<?= $order[0]['order_no'] ?></span>
                                                <?php if($order[0]['pay_status'] == 2 && $order[0]['order_status'] == 20):?>
                                                    <span class="am-margin-right-lg">交易号：<?= $order[0]['transaction_no'] ?></span>
                                                <?php endif;?>
                                                <span class="am-margin-right-lg">订单生成时间：<?= date('Y-m-d H:i:s', $order[0]['create_time']) ?></span>
                                            </td>
                                        </tr>
                                        <?php $goodsCount = count($order['OrderTicket']); $num = 0;?>

                                        <?php $name = 0; ?>
                                        <?php foreach ($order['OrderTicket'] as $data): ?>
                                            <?php $name += (int)$data['price'] * (int)$data['num']; ?>
                                        <?php endforeach;?>

                                        <?php foreach ($order['OrderTicket'] as $data): ?>
                                        <tr>
                                            <td class="goods-detail am-text-middle">
                                                <div class="goods-image">
                                                    <img src="/uploads/<?= $data['file_name']?>" onerror="this.src='/uploads/202207030914418f3b18004.jpeg'" alt="">
                                                </div>
                                                <div class="goods-info">
                                                    <p class="goods-title">商品名：</p>
                                                    <p><?= $data['name']?></p>
                                                </div>
                                            </td>
                                            <td class="am-text-middle">
                                                <p>￥<?= $data['price'] ?></p>
                                                <p>×<?= $data['num'] ?>(张)</p>
                                            </td>
                                            <td class="am-text-middle">
                                                <p><?php echo $data['user_name']; ?></p>
                                            </td>

                                            <?php if(  $goodsCount  > 1 ): ?>
                                                <?php if( $num == 0 ):?>
                                                    <td class="am-text-middle" rowspan="<?php echo 0; ?>" >
                                                        ￥<?php echo $name; ?>
                                                    </td>
                                                    <td class="am-text-middle" rowspan="<?php echo 0; ?>" ><?= $order[0]['order_price'] ?></td>
                                                    <td class="am-text-middle am-text-left" rowspan="<?php echo 0; ?>" >
                                                        订单状态：
                                                        <div class="am-badge am-badge-warning">
                                                            <?php
                                                            if( $order[0]['order_status'] == 10 )  {
                                                                echo '已下单 待支付';
                                                            }else if( $order[0]['order_status'] == 11 ){
                                                                echo '已下单后取消';
                                                            } else if( $order[0]['order_status'] == 20 ){
                                                                echo '已支付 待核销';
                                                            } else if( $order[0]['order_status'] == 21 ){
                                                                echo '已支付后取消';
                                                            } else if( $order[0]['order_status'] == 80 ){
                                                                echo '已完成';
                                                            }
                                                            ?>
                                                        </div>
                                                        <div class="am-margin-right-lg"></div>
                                                        付款状态：
                                                        <div class="am-badge <?= $order[0]['pay_status'] == 1 ? '' : 'am-badge-success' ?> ">
                                                            <?= $order[0]['pay_status'] == 1 ? '未付款' : '已付款' ?>
                                                        </div>
                                                    </td >
                                                    <td class="am-text-middle" rowspan="<?php echo 0; ?>" >
                                                        <div class="tpl-table-black-operation">
                                                            <?php if (checkPrivilege('orders/detail')): ?>
                                                                <a class="tpl-table-black-operation-green"
                                                                   href="<?= url('orders/detail', ['id' => $order[0]['id']]) ?>">订单详情</a>
                                                            <?php endif; ?>
                                                        </div>
                                                        <div class="tpl-table-black-operation">
                                                            <?php if (checkPrivilege('orders/payment')): ?>
                                                                <a href="javascript:;" class="item-delete tpl-table-black-operation-del"
                                                                   data-id="<?= $order[0]['id'] ?>">
                                                                    <i class="am-icon-trash"></i> 拆分
                                                                </a>
                                                            <?php endif; ?>
                                                        </div>
                                                    </td>
                                                    <?php $num = 1;  ?>
                                                <?php endif;?>
                                            <?php else: ?>
                                                <td class="am-text-middle">
                                                    <?php $name = 0; ?>
                                                    <?php $name += (int)$data['price'] * (int)$data['num']; ?>
                                                    ￥<?php echo $name; ?>
                                                </td>
                                                <td class="am-text-middle" ><?= $order[0]['order_price'] ?></td>
                                                <td class="am-text-middle am-text-left">
                                                    订单状态：
                                                    <div class="am-badge am-badge-warning">
                                                        <?php
                                                        if( $order[0]['order_status'] == 10 )  {

                                                            echo '已下单 待支付';
                                                        }else if( $order[0]['order_status'] == 11 ){
                                                            echo '已下单后取消';
                                                        } else if( $order[0]['order_status'] == 20 ){
                                                            echo '已支付 待核销';
                                                        } else if( $order[0]['order_status'] == 21 ){
                                                            echo '已支付后取消';
                                                        } else if( $order[0]['order_status'] == 80 ){
                                                            echo '已完成';
                                                        }
                                                        ?>
                                                    </div>
                                                    <div class="am-margin-right-lg"></div>
                                                    付款状态：
                                                    <div class="am-badge <?= $order[0]['pay_status'] == 1 ? '' : 'am-badge-success' ?> ">
                                                        <?= $order[0]['pay_status'] == 1 ? '未付款' : '已付款' ?>
                                                    </div>
                                                </td>
                                                <td class="am-text-middle" >
                                                    <div class="tpl-table-black-operation">
                                                        <?php if (checkPrivilege('orders/detail')): ?>
                                                            <a class="tpl-table-black-operation-green"
                                                               href="<?= url('orders/detail', ['id' => $order[0]['id']]) ?>">订单详情</a>
                                                        <?php endif; ?>
                                                    </div>
                                                    <div class="tpl-table-black-operation">
                                                        <?php if (checkPrivilege('orders/payment')): ?>
                                                            <a href="javascript:;" class="item-delete tpl-table-black-operation-del"
                                                               data-id="<?= $order[0]['id'] ?>">
                                                                <i class="am-icon-trash"></i> 拆分
                                                            </a>
                                                        <?php endif; ?>
                                                    </div>
                                                </td>
                                            <?php endif; ?>
                                        </tr>
                                        <?php endforeach;  ?>


                                    <?php else: ?>
                                        <tr>
                                            <td colspan="<?= $colspan ?>" class="am-text-center">暂无记录</td>
                                        </tr>
                                    <?php endif; ?>
                                <?php endforeach;  ?>

                            </tbody>
                        </table>
                    </div>

                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr"><?= $list->render() ?></div>
                        <div class="am-fr pagination-total am-margin-right">
                            <div class="am-vertical-align-middle">总记录：<?= $list->total() ?></div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<script>

    $(function () {

        /**
         * 订单导出
         */
        $('.j-export').click(function () {
            var data = {};
            var formData = $('#form-search').serializeArray();
            $.each(formData, function () {
                this.name !== 's' && (data[this.name] = this.value);
            });
            window.location = "<?= url('orders/export') ?>" + '&' + $.urlEncode(data);
        });

        var url = "<?= url('orders/payment') ?>";
        $('.item-delete').delete('id', url);

    });

</script>